//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.IO;
using System.Web.UI.WebControls;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Definitions;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Security;
using DotNetNuke.Services.Installer;
using DotNetNuke.Services.Installer.Packages;
using DotNetNuke.Services.Localization;
using DotNetNuke.UI.Modules;
using System.Text.RegularExpressions;
namespace DotNetNuke.Modules.Admin.ModuleDefinitions
{
	partial class CreateModuleDefinition : ModuleUserControlBase
	{
		private PackageInfo _Package = null;
		protected bool IsAddMode
		{
			get { return (PackageID == Null.NullInteger); }
		}
		protected PackageInfo Package
		{
			get
			{
				if (_Package == null)
				{
					if (PackageID == Null.NullInteger)
					{
						_Package = new PackageInfo();
					}
					else
					{
						_Package = PackageController.GetPackage(PackageID);
					}
				}
				return _Package;
			}
		}
		public int PackageID
		{
			get
			{
				int _PackageID = Null.NullInteger;
				if ((Request.QueryString["PackageID"] != null))
				{
					_PackageID = Int32.Parse(Request.QueryString["PackageID"]);
				}
				return _PackageID;
			}
		}
		private void AddFolder(string parentFolder, string newFolder)
		{
			string parentFolderPath = Common.Globals.ApplicationMapPath + "\\DesktopModules";
			if (!string.IsNullOrEmpty(parentFolder))
			{
				parentFolderPath += "\\" + parentFolder;
			}
			System.IO.DirectoryInfo dinfo = new System.IO.DirectoryInfo(parentFolderPath);
			System.IO.DirectoryInfo dinfoNew;
			dinfoNew = new System.IO.DirectoryInfo(parentFolderPath + "\\" + newFolder);
			if (!dinfoNew.Exists)
			{
				dinfoNew = dinfo.CreateSubdirectory(newFolder);
			}
		}
		private void BindControlList(string strRoot, bool blnRecurse)
		{
			string[] arrFolders;
			string[] arrFiles;
			if (Directory.Exists(Request.MapPath(Common.Globals.ApplicationPath + "/" + strRoot)))
			{
				arrFolders = Directory.GetDirectories(Request.MapPath(Common.Globals.ApplicationPath + "/" + strRoot));
				if (blnRecurse)
				{
					foreach (string strFolder in arrFolders)
					{
						BindControlList(strFolder.Substring(Request.MapPath(Common.Globals.ApplicationPath).Length + 1).Replace('\\', '/'), blnRecurse);
					}
				}
				arrFiles = Directory.GetFiles(Request.MapPath(Common.Globals.ApplicationPath + "/" + strRoot), "*.ascx");
				foreach (string strFile in arrFiles)
				{
					//strFile = strRoot.Replace('\\', '/') + "/" + Path.GetFileName(strFile);
				}
			}
		}
		private void BindManifestList(string strRoot, bool blnRecurse)
		{
			string[] arrFolders;
			string[] arrFiles;
			if (Directory.Exists(Request.MapPath(Common.Globals.ApplicationPath + "/" + strRoot)))
			{
				arrFolders = Directory.GetDirectories(Request.MapPath(Common.Globals.ApplicationPath + "/" + strRoot));
				if (blnRecurse)
				{
					foreach (string strFolder in arrFolders)
					{
						BindManifestList(strFolder.Substring(Request.MapPath(Common.Globals.ApplicationPath).Length + 1).Replace('\\', '/'), blnRecurse);
					}
				}
				arrFiles = Directory.GetFiles(Request.MapPath(Common.Globals.ApplicationPath + "/" + strRoot), "*.dnn");
				foreach (string strFile in arrFiles)
				{
				}
			}
		}

		private bool InvalidFilename(string fileName)
		{
            Regex invalidFilenameChars = new Regex("[" + Regex.Escape(new string(System.IO.Path.GetInvalidFileNameChars())) + "]");
			return invalidFilenameChars.IsMatch(fileName);
        }

		private string CreateControl(string controlSrc)
		{
			string folder = FileSystemUtils.RemoveTrailingSlash(GetSourceFolder());
			string className = GetClassName();
			string moduleControlPath = Server.MapPath("DesktopModules/" + folder + "/" + controlSrc);
			string message = Null.NullString;
			string source = string.Format(Localization.GetString("ModuleControlTemplate", this.LocalResourceFile), rblLanguage.SelectedValue, className);
			if (File.Exists(moduleControlPath))
			{
				message = Localization.GetString("FileExists", this.LocalResourceFile);
			}
			else
			{
				StreamWriter objStream;
				objStream = File.CreateText(moduleControlPath);
				objStream.WriteLine(source);
				objStream.Close();
			}
			return message;
		}
		private string GetClassName()
		{
			string strClass = Null.NullString;
			if (!String.IsNullOrEmpty(cboOwner.SelectedValue))
			{
				strClass += cboOwner.SelectedValue + ".";
			}
			if (!String.IsNullOrEmpty(cboModule.SelectedValue))
			{
				strClass += cboModule.SelectedValue;
			}
			//return class and remove any spaces that might appear in folder structure
			return strClass.Replace(" ", "");
		}
		private string GetSourceFolder()
		{
			string strFolder = Null.NullString;
			if (!String.IsNullOrEmpty(cboOwner.SelectedValue))
			{
				strFolder += cboOwner.SelectedValue + "/";
			}
			if (!String.IsNullOrEmpty(cboModule.SelectedValue))
			{
				strFolder += cboModule.SelectedValue + "/";
			}
			return strFolder;
		}
		private ModuleDefinitionInfo ImportControl(string controlSrc)
		{
			ModuleDefinitionInfo moduleDefinition = null;
			try
			{
				string folder = FileSystemUtils.RemoveTrailingSlash(GetSourceFolder());
				string friendlyName = txtName.Text;
				string name = GetClassName();
				string moduleControl = "DesktopModules/" + folder + "/" + controlSrc;
				PackageInfo package = new PackageInfo();
				package.Name = name;
				package.FriendlyName = friendlyName;
				package.Description = txtDescription.Text;
				package.Version = new Version(1, 0, 0);
				package.PackageType = "Module";
				package.License = Util.PACKAGE_NoLicense;
				PackageController.SavePackage(package);
				DesktopModuleController objDesktopModules = new DesktopModuleController();
				DesktopModuleInfo objDesktopModule = new DesktopModuleInfo();
				objDesktopModule.DesktopModuleID = Null.NullInteger;
				objDesktopModule.ModuleName = name;
				objDesktopModule.FolderName = folder;
				objDesktopModule.FriendlyName = friendlyName;
				objDesktopModule.Description = txtDescription.Text;
				objDesktopModule.IsPremium = false;
				objDesktopModule.IsAdmin = false;
				objDesktopModule.Version = "01.00.00";
				objDesktopModule.BusinessControllerClass = "";
				objDesktopModule.CompatibleVersions = "";
				objDesktopModule.Dependencies = "";
				objDesktopModule.Permissions = "";
				objDesktopModule.PackageID = package.PackageID;
				objDesktopModule.DesktopModuleID = objDesktopModules.AddDesktopModule(objDesktopModule);
				DesktopModuleController.AddDesktopModuleToPortals(objDesktopModule.DesktopModuleID);
				ModuleDefinitionController objModuleDefinitions = new ModuleDefinitionController();
				moduleDefinition = new ModuleDefinitionInfo();
				moduleDefinition.ModuleDefID = Null.NullInteger;
				moduleDefinition.DesktopModuleID = objDesktopModule.DesktopModuleID;
				moduleDefinition.FriendlyName = friendlyName;
				moduleDefinition.DefaultCacheTime = 0;
				moduleDefinition.ModuleDefID = objModuleDefinitions.AddModuleDefinition(moduleDefinition);
				ModuleControlController objModuleControls = new ModuleControlController();
				ModuleControlInfo objModuleControl = new ModuleControlInfo();
				objModuleControl.ModuleControlID = Null.NullInteger;
				objModuleControl.ModuleDefID = moduleDefinition.ModuleDefID;
				objModuleControl.ControlKey = "";
				objModuleControl.ControlSrc = moduleControl;
				objModuleControl.ControlTitle = "";
				objModuleControl.ControlType = SecurityAccessLevel.View;
				objModuleControl.HelpURL = "";
				objModuleControl.IconFile = "";
				objModuleControl.ViewOrder = 0;
				objModuleControl.SupportsPartialRendering = false;
				ModuleControlController.AddModuleControl(objModuleControl);
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.LogException(exc);
				DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Services.Localization.Localization.GetString("ImportControl.ErrorMessage", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
			}
			return moduleDefinition;
		}
		private ModuleDefinitionInfo ImportManifest()
		{
			ModuleDefinitionInfo moduleDefinition = null;
			try
			{
				string folder = FileSystemUtils.RemoveTrailingSlash(GetSourceFolder());
				string manifest = Server.MapPath("~/DesktopModules/" + folder + "/" + cboFile.SelectedValue);
				Installer _Installer = new Installer(manifest, Request.MapPath("."), true);
				if (_Installer.IsValid)
				{
					_Installer.InstallerInfo.Log.Logs.Clear();
					_Installer.Install();
					if (_Installer.IsValid)
					{
						DesktopModuleInfo desktopModule = DesktopModuleController.GetDesktopModuleByPackageID(_Installer.InstallerInfo.PackageID);
						if (desktopModule != null && desktopModule.ModuleDefinitions.Count > 0)
						{
							foreach (KeyValuePair<string, ModuleDefinitionInfo> kvp in desktopModule.ModuleDefinitions)
							{
								moduleDefinition = kvp.Value;
								break;
							}
						}
					}
					else
					{
						DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Services.Localization.Localization.GetString("InstallError.Text", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
						phInstallLogs.Controls.Add(_Installer.InstallerInfo.Log.GetLogsTable());
					}
				}
				else
				{
					DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Services.Localization.Localization.GetString("InstallError.Text", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
					phInstallLogs.Controls.Add(_Installer.InstallerInfo.Log.GetLogsTable());
				}
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.LogException(exc);
				DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, Services.Localization.Localization.GetString("ImportControl.ErrorMessage", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
			}
			return moduleDefinition;
		}
		private void LoadFiles(string strExtensions)
		{
			LoadFiles(strExtensions, "");
		}
		private void LoadFiles(string strExtensions, string strFolder)
		{
			if (String.IsNullOrEmpty(strFolder))
			{
				strFolder = Server.MapPath("~/DesktopModules/" + GetSourceFolder());
			}
			cboFile.Items.Clear();
			string[] arrFiles = Directory.GetFiles(strFolder);
			foreach (string strFile in arrFiles)
			{
				if (strExtensions.Contains(Path.GetExtension(strFile)))
				{
					cboFile.Items.Add(Path.GetFileName(strFile));
				}
			}
		}
		private void LoadModuleFolders(string selectedValue)
		{
			cboModule.Items.Clear();
			string[] arrFolders = Directory.GetDirectories(Common.Globals.ApplicationMapPath + "\\DesktopModules\\" + cboOwner.SelectedValue);
			foreach (string strFolder in arrFolders)
			{
				ListItem item = new ListItem(strFolder.Replace(Path.GetDirectoryName(strFolder) + "\\", ""));
				if (item.Value == selectedValue)
				{
					item.Selected = true;
				}
				cboModule.Items.Add(item);
			}
			cboModule.Items.Insert(0, new ListItem("<" + Services.Localization.Localization.GetString("Not_Specified", Services.Localization.Localization.SharedResourceFile) + ">", ""));
		}
		private void LoadOwnerFolders(string selectedValue)
		{
			cboOwner.Items.Clear();
			string[] arrFolders = Directory.GetDirectories(Common.Globals.ApplicationMapPath + "\\DesktopModules\\");
			foreach (string strFolder in arrFolders)
			{
				string[] files = Directory.GetFiles(strFolder, "*.ascx");
				if (files.Length == 0 || strFolder.ToLower() == "admin")
				{
					ListItem item = new ListItem(strFolder.Replace(Path.GetDirectoryName(strFolder) + "\\", ""));
					if (item.Value == selectedValue)
					{
						item.Selected = true;
					}
					cboOwner.Items.Add(item);
				}
			}
			cboOwner.Items.Insert(0, new ListItem("<" + Services.Localization.Localization.GetString("Not_Specified", Services.Localization.Localization.SharedResourceFile) + ">", ""));
		}
		private void SetupModuleFolders()
		{
			switch (cboCreate.SelectedValue)
			{
				case "Control":
					LoadFiles(".ascx");
					break;
				case "Template":
					LoadFiles(".module.template", Common.Globals.HostMapPath + "Templates\\");
					break;
				case "Manifest":
					LoadFiles(".dnn,.dnn5");
					break;
			}
		}
		private void SetupOwnerFolders()
		{
			LoadModuleFolders(Null.NullString);
			SetupModuleFolders();
		}
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			if (IsAddMode)
			{
				ModuleContext.Configuration.ModuleTitle = Services.Localization.Localization.GetString("Add.Title", this.LocalResourceFile);
			}
			else
			{
				ModuleContext.Configuration.ModuleTitle = Services.Localization.Localization.GetString("Edit.Title", this.LocalResourceFile);
			}
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cboCreate.SelectedIndexChanged += new EventHandler(cboCreate_SelectedIndexChanged);
			cboModule.SelectedIndexChanged += new EventHandler(cboModule_SelectedIndexChanged);
			cboOwner.SelectedIndexChanged += new EventHandler(cboOwner_SelectedIndexChanged);
			cmdAddModule.Click += new EventHandler(cmdAddModule_Click);
			cmdAddOwner.Click += new EventHandler(cmdAddOwner_Click);
			cmdCancel.Click += new EventHandler(cmdCancel_Click);
			cmdCancelModule.Click += new EventHandler(cmdCancelModule_Click);
			cmdCancelOwner.Click += new EventHandler(cmdCancelOwner_Click);
			cmdCreate.Click += new EventHandler(cmdCreate_Click);
			cmdSaveModule.Click += new EventHandler(cmdSaveModule_Click);
			cmdSaveOwner.Click += new EventHandler(cmdSaveOwner_Click);

			try
			{
				if (!Page.IsPostBack)
				{
					cboCreate.Items.Insert(0, new ListItem("<" + Services.Localization.Localization.GetString("None_Specified") + ">", ""));
					LoadOwnerFolders(Null.NullString);
					LoadModuleFolders(Null.NullString);
				}
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		protected void cboCreate_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			LoadOwnerFolders(Null.NullString);
			LoadModuleFolders(Null.NullString);
			cboFile.Items.Clear();
			txtModule.Text = "";
			txtDescription.Text = "";
			switch (cboCreate.SelectedValue)
			{
				case "":
					rowOwner1.Visible = false;
					cmdAddOwner.Visible = false;
					rowModule1.Visible = false;
					cmdAddModule.Visible = false;
					rowFile1.Visible = false;
					rowFile2.Visible = false;
					rowLang.Visible = false;
					rowName.Visible = false;
					rowDescription.Visible = false;
					rowSource.Visible = false;
					rowAddPage.Visible = false;
					cmdCreate.Visible = false;
					break;
				case "New":
					rowOwner1.Visible = true;
					cmdAddOwner.Visible = true;
					rowModule1.Visible = true;
					cmdAddModule.Visible = true;
					rowFile1.Visible = false;
					rowFile2.Visible = true;
					rowLang.Visible = true;
					rowName.Visible = true;
					rowDescription.Visible = true;
					rowSource.Visible = false;
					rowAddPage.Visible = true;
					cmdCreate.Visible = true;
					break;
				case "Control":
					rowOwner1.Visible = true;
					cmdAddOwner.Visible = false;
					rowModule1.Visible = true;
					cmdAddModule.Visible = false;
					rowFile1.Visible = true;
					rowFile2.Visible = false;
					rowLang.Visible = false;
					rowName.Visible = true;
					rowDescription.Visible = true;
					rowSource.Visible = false;
					rowAddPage.Visible = true;
					cmdCreate.Visible = true;
					break;
				case "Template":
					rowOwner1.Visible = true;
					cmdAddOwner.Visible = true;
					rowModule1.Visible = true;
					cmdAddModule.Visible = true;
					rowFile1.Visible = true;
					rowFile2.Visible = false;
					rowLang.Visible = false;
					rowName.Visible = true;
					rowDescription.Visible = true;
					rowSource.Visible = false;
					rowAddPage.Visible = false;
					cmdCreate.Visible = true;
					break;
				case "Manifest":
					rowOwner1.Visible = true;
					cmdAddOwner.Visible = false;
					rowModule1.Visible = true;
					cmdAddModule.Visible = false;
					rowFile1.Visible = true;
					rowFile2.Visible = false;
					rowLang.Visible = false;
					rowName.Visible = false;
					rowDescription.Visible = false;
					rowSource.Visible = false;
					rowAddPage.Visible = true;
					cmdCreate.Visible = true;
					break;
			}
		}
		protected void cboModule_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			SetupModuleFolders();
		}
		protected void cboOwner_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			SetupOwnerFolders();
		}
		protected void cmdAddModule_Click(object sender, System.EventArgs e)
		{
			rowModule1.Visible = false;
			rowModule2.Visible = true;
		}
		protected void cmdAddOwner_Click(object sender, System.EventArgs e)
		{
			rowOwner1.Visible = false;
			rowOwner2.Visible = true;
		}
		protected void cmdCancel_Click(object sender, EventArgs e)
		{
			try
			{
				Response.Redirect(Common.Globals.NavigateURL(), true);
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
		protected void cmdCancelModule_Click(object sender, System.EventArgs e)
		{
			rowModule1.Visible = true;
			rowModule2.Visible = false;
		}
		protected void cmdCancelOwner_Click(object sender, System.EventArgs e)
		{
			rowOwner1.Visible = true;
			rowOwner2.Visible = false;
		}
		protected void cmdCreate_Click(object sender, System.EventArgs e)
		{
			try
			{
				ModuleDefinitionInfo moduleDefinition = null;
				string strMessage = Null.NullString;
				switch (cboCreate.SelectedValue)
				{
					case "":
						break;
					case "New":
						if(String.IsNullOrEmpty(cboModule.SelectedValue))
						{
                            strMessage = Localization.GetString("ModuleFolder", this.LocalResourceFile);
                            break;
                        }

                        if(String.IsNullOrEmpty(rblLanguage.SelectedValue))
						{
                            strMessage = Localization.GetString("LanguageError", this.LocalResourceFile);
                            break;
                        }

                        //remove spaces so file is created correctly
                        string controlSrc = txtFile.Text.Replace(" ", "");
                        if(InvalidFilename(controlSrc))
						{
                            strMessage = Localization.GetString("InvalidFilename", this.LocalResourceFile);
                            break;
                        }

                        if(String.IsNullOrEmpty(controlSrc))
						{
                            strMessage = Localization.GetString("MissingControl", this.LocalResourceFile);
                            break;
                        }
                        if(String.IsNullOrEmpty(txtName.Text))
						{
                            strMessage = Localization.GetString("MissingFriendlyname", this.LocalResourceFile);
                            break;
                        }
                        if(!controlSrc.EndsWith(".ascx"))
						{
                            controlSrc += ".ascx";
                        }

                        bool uniqueName = true;
                        List<PackageInfo> packages = new List<PackageInfo>();
                        foreach(PackageInfo package in PackageController.GetPackages())
						{
                            if(package.Name == txtName.Text || package.FriendlyName == txtName.Text)
							{
                                uniqueName = false;
                                break;
                            }
                        }

                        if(!uniqueName)
						{
                            strMessage = Localization.GetString("NonuniqueName", this.LocalResourceFile);
                            break;
                        }
                        //First create the control
                        strMessage = CreateControl(controlSrc);
                        if(String.IsNullOrEmpty(strMessage))
						{
                            //Next import the control
							moduleDefinition = ImportControl(controlSrc);
                        }
						break;
					case "Control":
						if (!String.IsNullOrEmpty(cboFile.SelectedValue))
						{
                            moduleDefinition = ImportControl(cboFile.SelectedValue);
                        }
						else
						{
							strMessage = Localization.GetString("NoControl", this.LocalResourceFile);
                        }
						break;
					case "Template":
						break;
					case "Manifest":
						if(String.IsNullOrEmpty(cboFile.SelectedValue))
						{
							strMessage = Localization.GetString("MissingManifest", this.LocalResourceFile);
                        }
						break;
				}
				if (moduleDefinition == null)
				{
					DotNetNuke.UI.Skins.Skin.AddModuleMessage(this, strMessage, DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
				}
				else
				{
					if (!string.IsNullOrEmpty(cboCreate.SelectedValue) && chkAddPage.Checked)
					{
						string tabName = "Test " + txtName.Text + " Page";
						string tabPath = Common.Globals.GenerateTabPath(Null.NullInteger, tabName);
						int tabID = TabController.GetTabByTabPath(this.ModuleContext.PortalId, tabPath);
						if (tabID == Null.NullInteger)
						{
							DotNetNuke.Entities.Tabs.TabInfo newTab = new DotNetNuke.Entities.Tabs.TabInfo();
							newTab.TabName = "Test " + txtName.Text + " Page";
							newTab.ParentId = Null.NullInteger;
							newTab.PortalID = this.ModuleContext.PortalId;
							newTab.IsVisible = true;
							newTab.TabID = new TabController().AddTabBefore(newTab, this.ModuleContext.PortalSettings.AdminTabId);
							ModuleInfo objModule = new ModuleInfo();
							objModule.Initialize(this.ModuleContext.PortalId);
							objModule.PortalID = this.ModuleContext.PortalId;
							objModule.TabID = newTab.TabID;
							objModule.ModuleOrder = Null.NullInteger;
							objModule.ModuleTitle = moduleDefinition.FriendlyName;
							objModule.PaneName = Common.Globals.glbDefaultPane;
							objModule.ModuleDefID = moduleDefinition.ModuleDefID;
							objModule.InheritViewPermissions = true;
							objModule.AllTabs = false;
							ModuleController moduleCtl = new ModuleController();
							moduleCtl.AddModule(objModule);
							Response.Redirect(Common.Globals.NavigateURL(newTab.TabID), true);
						}
						else
						{
							Skin.AddModuleMessage(this, Localization.GetString("TabExists", this.LocalResourceFile), DotNetNuke.UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
						}
					}
					else
					{
						Response.Redirect(Common.Globals.NavigateURL(), true);
					}
				}
			}
			catch (Exception ex)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, ex);
			}
		}
		protected void cmdSaveModule_Click(object sender, System.EventArgs e)
		{
			AddFolder(cboOwner.SelectedValue, txtModule.Text);
			LoadModuleFolders(txtModule.Text);
			SetupModuleFolders();
			rowModule1.Visible = true;
			rowModule2.Visible = false;
		}
		protected void cmdSaveOwner_Click(object sender, System.EventArgs e)
		{
			AddFolder("", txtOwner.Text);
			LoadOwnerFolders(txtOwner.Text);
			SetupOwnerFolders();
			rowOwner1.Visible = true;
			rowOwner2.Visible = false;
		}
	}
}
